﻿using System.Linq;
using System.Web.Mvc;
using SqlSugar;
using Xwltz.Framework.Framework;
using Xwltz.Framework.Helper;

namespace Xwltz.Framework.Database
{
    public class DatabaseContext : Controller
    {
        /// <summary>
        /// 初始化数据库对象
        /// </summary>
        protected SqlSugarClient Db;

        #region 构造函数

        public DatabaseContext()
        {
            Db = GetInstance();
        }

        public DatabaseContext(bool showSql)
        {
            Db = GetInstance(showSql);
        }

        public DatabaseContext(bool showSql, DbType sqlType)
        {
            Db = GetInstance(showSql, sqlType);
        }

        #endregion

        /// <summary>
        /// 获取数据库实例
        /// Ps:事务只能在同一个SqlSugarClient对象有效
        /// 数据库链接请修改ConnectionStrings["SqlServerStr"]的值
        /// 如果为多数据库或复杂场景请自己封装
        /// </summary>
        /// <returns></returns>
        public SqlSugarClient GetInstance(bool showSql = false, DbType sqlType = DbType.SqlServer, bool isRedis = false)
        {
            var conn = new ConnectionConfig()
            {
                // 数据库链接
                ConnectionString = FrameworkConfigs.ConnectionString,
                // 数据库类型
                DbType = sqlType,
                // 是否自动关闭链接
                IsAutoCloseConnection = true
            };

            var cacheContrioner = new ConfigureExternalServices();
            if (isRedis)
            {
                cacheContrioner.DataInfoCacheService = new RedisCache();
            }
            else
            {
                cacheContrioner.DataInfoCacheService = new HttpRuntimeCache();
            }

            conn.ConfigureExternalServices = cacheContrioner;

            var db = new SqlSugarClient(conn);

            if (showSql)
            {
                db.Aop.OnLogExecuting = (sql, pars) =>
                {
                    System.Diagnostics.Debug.WriteLine(sql + "\r\n" + db.Utilities.SerializeObject(pars.ToDictionary(it => it.ParameterName, it => it.Value)));
                };
            }
            return db;
        }

        /// <summary>
        /// 获取数据库实例
        /// Ps:事务只能在同一个SqlSugarClient对象有效
        /// 数据库链接请修改ConnectionStrings["SqlServerStr"]的值
        /// 如果为多数据库或复杂场景请自己封装
        /// </summary>
        /// <returns></returns>
        public SqlSugarClient GetInstance(string connStr, bool showSql = false, DbType sqlType = DbType.SqlServer, bool isRedis = false, bool isAutoClose = true)
        {
            var conn = new ConnectionConfig()
            {
                // 数据库链接
                ConnectionString = connStr,
                // 数据库类型
                DbType = sqlType,
                // 是否自动关闭链接
                IsAutoCloseConnection = isAutoClose
            };

            var cacheContrioner = new ConfigureExternalServices();
            if (isRedis)
            {
                cacheContrioner.DataInfoCacheService = new RedisCache();
            }
            else
            {
                cacheContrioner.DataInfoCacheService = new HttpRuntimeCache();
            }

            conn.ConfigureExternalServices = cacheContrioner;

            var db = new SqlSugarClient(conn);

            if (showSql)
            {
                db.Aop.OnLogExecuting = (sql, pars) =>
                {
                    System.Diagnostics.Debug.WriteLine(sql + "\r\n" + db.Utilities.SerializeObject(pars.ToDictionary(it => it.ParameterName, it => it.Value)));
                };
            }
            return db;
        }
    }
}
